<?php

namespace App\Handler\Admin\Member\Member;

if (!defined('IN_PX'))
    exit;

use App\Handler\Admin\AbstractCommon;
use App\Admin\Helper;
use App\Tools\Auxi;
use Phoenix\Support\MsgHelper;

/**
 * 读取
 */
class Read extends AbstractCommon {

    public function processRequest(Array & $context) {
        $this->_pushSetting();

        if (!$_POST['sortName'])
            $_POST['sortName'] = 'm.member_id';
        if (!$_POST['sortOrder'])
            $_POST['sortOrder'] = 'DESC';

        if (!$_POST['page'])
            $_POST['page'] = 1;
        if (!$_POST['rp'])
            $_POST['rp'] = 10;
        $_start = (($_POST['page'] - 1) * $_POST['rp']);

        $_where = '0 = 0';
        $_bindParam = array();

        if (isset($_POST['sltDateA']) && $_POST['sltDateA'] && $_POST['sltDateB']) {
            $_where .= ' AND (m.`release_date` BETWEEN :sltDateA AND :sltDateB)';
            $_bindParam[':sltDateA'] = $_POST['sltDateA'];
            $_bindParam[':sltDateB'] = $_POST['sltDateB'];
        }

        if (isset($_POST['strSearchKeyword']) && $_POST['strSearchKeyword'] != '') {
            $_where .= ' AND ( (m.`member_name` LIKE :strSearchKeyword) 
            OR (m.`mobile` LIKE :strSearchKeyword))';
            $_bindParam[':strSearchKeyword'] = '%' . trim($_POST['strSearchKeyword']) . '%';
        }

        $_table = '`#@__@member` m LEFT JOIN `#@__@manager_user` mu ON m.`master_id` = mu.`user_id`';
        $_total = $this->db->table($_table)->where($_where)->bind($_bindParam)->count();
        $this->db->debug();
        $_rs = $this->db->select('m.*, mu.`real_name`')
            ->table($_table)
            ->where($_where)
            ->order($_POST['sortName'], $_POST['sortOrder'])
            ->limit($_start, $_POST['rp'])
            ->bind($_bindParam)
            ->findAll();

        $_rsp = array(
            'totalResults' => $_total,
            'rows' => array()
        );
        if ($_total) {
            foreach ($_rs as $m) {
                $_idValue = $m->member_id;
                array_push($_rsp['rows'], array(
                    'id' => $_idValue,
                    'cell' => array(
                        $_idValue,
                        $m->member_name,
                        $m->mobile,
                        '<span' . Auxi::getDeepColor($m->sex) . '>' .
                        $this->setting['arySex'][intval($m->sex)] . '</span>',
                        $m->openid ? '<div title="'. $m->openid .'">已绑定</div>' : '-',
//                        '<div style="text-align: center; width: 100px;"><a href="' . ($m->portrait ? : '/uploads/no_img.gif') .'" class="mybox" title=""><img src="'.$m->portrait.'" height="20" onload="if(this.width/this.height>3){this.width=50;this.removeAttribute(\'height\');}"></a></div>',
                        $this->setting['aryMemberType'][intval($m->department_id)],
                        $m->location,
                        '<span' . Auxi::getDeepColor($m->is_status) . '>' .
                        $this->setting['aryMemberStatus'][intval($m->is_status)] . '</span>',
                        date('Y-m-d H:i:s', $m->last_log_date),
                        date('Y-m-d H:i:s', $m->release_date),
                        $m->real_name,
                        $m->remarks,
                    )
                ));
            }
        }
        echo(MsgHelper::json('SUCCESS', '数据返回成功', $_rsp));
    }

}
